<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Cache extends MY_Front_Controller {
	
	/**
	 * 
	 * The constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->load->model('cache_model');
		$this->load->helper('file');
	}	
	
	/**
	 * 
	 * Clears the cache
	 */
	public function clear(){
		if (ob_get_level() == 0){
			ob_start();			
		}		
		echo 'Processing ' . $this->cache_model->countAll() . ' cache records .... please wait it will take a few minutes.';
        ob_flush();
	    flush();		
		$cacheData = $this->cache_model->getAll();
		foreach ($cacheData as $cache) {
			if (file_exists($cache->path)) {
				$result = $this->cache_model->executeQuery(unserialize($cache->query));	
				$result->result();
				write_file($cache->path, serialize($result));				
			}
		}
		echo '<br />The Cache System has been updated.';
		ob_end_flush();
		
	}
}